package com.stardata.liangshisq.tcslmon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stardata.liangshisq.tcslmon.entity.BizBiOrderDay;
import com.stardata.liangshisq.tcslmon.mapper.BizBiOrderDayMapper;
import com.stardata.liangshisq.tcslmon.service.BizBiOrderDayService;
import com.stardata.liangshisq.tcslmon.service.SysParameterService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class BizBiOrderDayServiceImpl extends ServiceImpl<BizBiOrderDayMapper, BizBiOrderDay> implements BizBiOrderDayService {

    private final BizBiOrderDayMapper bizBiOrderDayMapper;

    private final SysParameterService parameterService;

    @Override
    public BizBiOrderDay getBizOrderBi(Long shopId) {
        String kscHisTime = parameterService.getByCode("kscHisTime");
        QueryWrapper<BizBiOrderDay> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_id", shopId)
                .eq("settle_biz_date", LocalDate.now()) ;
        BizBiOrderDay bizBiOrderDay = bizBiOrderDayMapper.selectOne(wrapper);
        if(bizBiOrderDay==null){
            bizBiOrderDay = new BizBiOrderDay();
            bizBiOrderDay.setBizNum(BigDecimal.ZERO);
            bizBiOrderDay.setBizTimeoutNum(BigDecimal.ZERO);

            bizBiOrderDay.setKscNum(BigDecimal.ZERO);
            bizBiOrderDay.setKscTimeoutNum(BigDecimal.ZERO);
            bizBiOrderDay.setKscTimeoutPer(BigDecimal.ZERO);
            bizBiOrderDay.setKscTime(BigDecimal.ZERO);
            bizBiOrderDay.setKscHisTime(new BigDecimal(kscHisTime));
            return bizBiOrderDay;
        }else{
            bizBiOrderDay.setKscHisTime(new BigDecimal(kscHisTime));

            if(bizBiOrderDay.getKscNum().compareTo(BigDecimal.ZERO)!=0){
                BigDecimal  timeOutPer = bizBiOrderDay.getKscTimeoutNum().divide(bizBiOrderDay.getKscNum(),BigDecimal.ROUND_HALF_UP);
                bizBiOrderDay.setKscTimeoutPer(timeOutPer.multiply(new BigDecimal(100)));
            }else{
                bizBiOrderDay.setKscTime(BigDecimal.ZERO);
                bizBiOrderDay.setKscTimeoutPer(BigDecimal.ZERO.multiply(new BigDecimal(100)));
            }
        }
        return bizBiOrderDay ;
    }

    @Override
    public List<BizBiOrderDay> getBizHisBi(Long shopId) {
        String kscHisTime = parameterService.getByCode("kscHisTime");
        QueryWrapper<BizBiOrderDay> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_id", shopId)
                .between("settle_biz_date", LocalDate.now().minusDays(7),LocalDate.now())
                .orderBy(true,false, "settle_biz_date");

        List<BizBiOrderDay> bizBiOrderDayList = bizBiOrderDayMapper.selectList(wrapper);
        List<BizBiOrderDay>  result = new ArrayList<>();
        for(BizBiOrderDay bizBiOrderDay:bizBiOrderDayList){
            bizBiOrderDay.setKscHisTime(new BigDecimal(kscHisTime));
            if(bizBiOrderDay.getKscNum().compareTo(BigDecimal.ZERO)!=0){
                BigDecimal  timeOutPer = bizBiOrderDay.getKscTimeoutNum().divide(bizBiOrderDay.getKscNum(),4,BigDecimal.ROUND_HALF_UP);
                bizBiOrderDay.setKscTimeoutPer(timeOutPer.multiply(new BigDecimal(100)));
            }else{
                bizBiOrderDay.setKscTime(BigDecimal.ZERO);
                bizBiOrderDay.setKscTimeoutPer(BigDecimal.ZERO.multiply(new BigDecimal(100)));
            }
            result.add(bizBiOrderDay);
        }
        return result ;
    }
}
